package com.eduardcapell.lopodder.beans;

import com.eduardcapell.lopodder.gui.FrameApplication;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class FeedElement extends Observable implements Comparable {
	private int id;
	private Feed feed;
	private String name;
	private Date date;
	private long size;
	private String description;
	private boolean downloaded = false;
	private URL url = null;
	private String contentType = null;
    private long position = 0;
    private String fileName = null;
    
	
	List<Observer> observers = new ArrayList<Observer>();
	
	@Override
	public synchronized void addObserver(Observer o) {
		observers.add(o);
	}
	
	
	public int getId() {
		return id;
	}

	@Override
	public boolean equals(Object o) {
		if (o == null || !(o instanceof FeedElement)) {
			return false;
		}
		
		FeedElement fe = (FeedElement) o;
		
		if (fe.getUrl() == null) {
			return (getUrl() == null);
		}
		
		return fe.getUrl().equals(getUrl());
	}

	@Override
	public int hashCode() {
		return getUrl().hashCode();
	}

	@Override
	public String toString() {
		return getUrl()==null?"NO_URL ":getUrl() + " - " + getName();
	}

	public void setId(int id) {
		this.id = id;
	}
	
	
	public boolean isDownloaded() {
		return downloaded;
	}

	public void setDownloaded(boolean downloaded) {
		this.downloaded = downloaded;
		notifyObservers();
	}


	@Override
	public void notifyObservers() {
		Iterator<Observer> it = observers.iterator();
		while (it.hasNext()) {
			Observer o = it.next();
			o.update(this, null);
		}
	}


	@Override
	public synchronized void deleteObservers() {
		observers.clear();
	}

	public FeedElement() {
	}

	public Feed getFeed() {
		return feed;
	}

	public void setFeed(Feed feed) {
		this.feed = feed;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public URL getUrl() {
		return url;
	}

	public void setUrl(URL url) {
		this.url = url;
	}

	public String getContenType() {
		return contentType;
	}

	public void setContenType(String contenType) {
		this.contentType = contenType;
	}


	public int compareTo(Object o) {
		if (o == null || ! (o instanceof FeedElement)) {
			return -1;
		}
		
		FeedElement fe = (FeedElement) o;
		
		if (fe.getDate() == null) {
			return -1;
		}
		
		if (getDate() == null) {
			return 1;
		}
		
		return (getDate().compareTo(fe.getDate())) * -1;
	}

    /**
     * @return the position
     */
    public long getPosition() {
        return position;
    }

    /**
     * @param position the position to set
     */
    public void setPosition(long position) {
        this.position = position;
    }

    /**
     * @return the fileName
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * @param fileName the fileName to set
     */
    public void setFileName(String fileName) {
        this.fileName = fileName;
        
    }

	
}
